package nckd.wnqjt.tmc.tda.plugin.form;

import kd.bos.algo.DataSet;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.FilterItemInfo;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhujintao
 * @date 2024/11/05
 * @desc 融资成本
 */
public class FinanceCostsListDataPlugin extends AbstractReportListDataPlugin {
    //提款中,已提款,已部分还款
    private final String[] drawTypes = {"drawed","partpayment"};

    @Override
    public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable {

        List<QFilter> filters = new ArrayList<>();
        //获取过滤条件
        List<FilterItemInfo> filterItems = reportQueryParam.getFilter().getFilterItems();
        filterItems.forEach(item->{
            if ("nckd_org".equals(item.getPropName()) && item.getValue() != null){
                //DynamicObject org = (DynamicObject) item.getValue();
                //QFilter orgFilter = new QFilter("org.id", QCP.equals,org.getPkValue());
                //filters.add(orgFilter);
                Object value = item.getValue();
                DynamicObjectCollection nckdOrg = null;
                if (value != null) {
                    nckdOrg = (DynamicObjectCollection) value;
                    List<Long> longList = nckdOrg.stream().map(e -> e.getLong("id")).collect(Collectors.toList());
                    QFilter orgFilter = new QFilter("org.id", QCP.in, longList);
                    filters.add(orgFilter);
                }
            }
        });
        //单据状态=已审核
        filters.add(new QFilter("billstatus",QCP.equals,"C"));
        //确认状态=已确认
        filters.add(new QFilter("confirmstatus",QCP.equals,"yetconfirm"));
        //债权人类型=银行
        QFilter bankFilter = new QFilter("creditortype", QCP.equals, "bank");
        //提款业务状态=已提款
        QFilter drawFilter = new QFilter("drawtype", QCP.in,drawTypes);
        filters.add(bankFilter);
        filters.add(drawFilter);

        String filed = "id,billstatus,confirmstatus,creditortype,drawtype,org,org.id,org.name as nckd_orgf,textcreditor as nckd_bank,notrepayamount as nckd_total,startloanrate as nckd_rate,expiredate";
        DataSet dataSet = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cfm_loanbill", filed, filters.toArray(new QFilter[filters.size()]), null);
        dataSet = dataSet.select("id,nckd_orgf,nckd_bank,nckd_rate,(nckd_total/10000) as nckd_total,((nckd_total * (nckd_rate / 100))/10000) as nckd_interest,1 as count,case when expiredate < Date() then '2' else '1' end as nckd_hig");

        //增加汇总行
        DataSet total = dataSet.select("id,null as nckd_orgf, concat('合计') as nckd_bank, 0 as nckd_rate, nckd_total,nckd_interest");
        total = total.groupBy(new String[]{"nckd_bank"}).sum("nckd_total").sum("nckd_interest").finish().select("null as id,null as nckd_orgf,nckd_bank,((nckd_interest / nckd_total) * 100) as nckd_rate,nckd_total,nckd_interest,2 as count,'1' as nckd_hig");

        //加权贷款利率行
        //DataSet rateDataSet = total.select("null as id,null as nckd_orgf,concat('加权贷款利率') as nckd_bank,0 as nckd_rate,nckd_total,(nckd_interest / nckd_total) as nckd_interest,3 as count,'1' as nckd_hig");
        total = total.union(dataSet).orderBy(new String[]{"count"});
        return total;
    }
}